{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Importing packages\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import sklearn\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn import preprocessing\n",
    "import statsmodels.api as sm\n",
    "import seaborn as sns; sns.set(color_codes=True)\n",
    "import math\n",
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Parameters\n",
    "\n",
    "v=10\n",
    "lam=.125\n",
    "mu=100000\n",
    "c=1\n",
    "\n",
    "start=1\n",
    "end=1001\n",
    "inc=float(1)/(end-start)\n",
    "\n",
    "endow=10\n",
    "epsilon=.1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Period Functions**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def period10(oldprobDraw,piold):\n",
    "    draws=[[],[],[],[],[],[],[],[],[],[]]\n",
    "    payoffs=[[],[],[],[],[],[],[],[],[],[]]\n",
    "\n",
    "    for x in range(0,10):\n",
    "        for i in range(1001):\n",
    "            vDraw=endow+((v-c*(x+1))*(piold[i]*(.001*i)+sum([(.001)*piold[j] for j in range(i+1,1001)])))+((-lamb*(x+1))*(1-(piold[i]*(.001*i)+sum([(.001)*piold[j] for j in range(i+1,1001)]))))\n",
    "            vNoDraw=endow+((v-c*x)*piold[i]+(-lamb*x)*(1-piold[i]))\n",
    "            probDraw=np.exp(mu*(vDraw-max(vDraw,vNoDraw)))/(np.exp(mu*(vDraw-max(vDraw,vNoDraw)))+np.exp(mu*(vNoDraw-max(vDraw,vNoDraw))))\n",
    "            draws[x].append((epsilon)*probDraw+(1-epsilon)*(oldprobDraw[x][i]))\n",
    "            payoffs[x].append(vDraw*(probDraw)+vNoDraw*(1-probDraw))\n",
    "            \n",
    "    return [draws,payoffs]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def period9(oldprobDraw,piold,payoffs10):\n",
    "    draws=[[],[],[],[],[],[],[],[],[]]\n",
    "    payoffs=[[],[],[],[],[],[],[],[],[]]\n",
    "\n",
    "    for x in range(0,9):\n",
    "        for i in range(1001):\n",
    "            vDraw=payoffs10[x+1][i]*(.001*i)+sum([(.001)*payoffs10[x+1][j] for j in range(i+1,1001)])\n",
    "            vNoDraw=payoffs10[x][i]\n",
    "            probDraw=np.exp(mu*(vDraw-max(vDraw,vNoDraw)))/(np.exp(mu*(vDraw-max(vDraw,vNoDraw)))+np.exp(mu*(vNoDraw-max(vDraw,vNoDraw))))\n",
    "            draws[x].append((epsilon)*probDraw+(1-epsilon)*(oldprobDraw[x][i]))\n",
    "            payoffs[x].append(vDraw*(probDraw)+vNoDraw*(1-probDraw))  \n",
    "\n",
    "    \n",
    "    return [draws,payoffs]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def period8(oldprobDraw,piold,payoffs9):\n",
    "    draws=[[],[],[],[],[],[],[],[]]\n",
    "    payoffs=[[],[],[],[],[],[],[],[]]\n",
    "\n",
    "    for x in range(0,8):\n",
    "        for i in range(1001):\n",
    "            vDraw=payoffs9[x+1][i]*(.001*i)+sum([(.001)*payoffs9[x+1][j] for j in range(i+1,1001)])\n",
    "            vNoDraw=payoffs9[x][i]\n",
    "            probDraw=np.exp(mu*(vDraw-max(vDraw,vNoDraw)))/(np.exp(mu*(vDraw-max(vDraw,vNoDraw)))+np.exp(mu*(vNoDraw-max(vDraw,vNoDraw))))\n",
    "            draws[x].append((epsilon)*probDraw+(1-epsilon)*(oldprobDraw[x][i]))\n",
    "            payoffs[x].append(vDraw*(probDraw)+vNoDraw*(1-probDraw))  \n",
    "\n",
    "    \n",
    "    return [draws,payoffs]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def period7(oldprobDraw,piold,payoffs8):\n",
    "    draws=[[],[],[],[],[],[],[]]\n",
    "    payoffs=[[],[],[],[],[],[],[]]\n",
    "\n",
    "    for x in range(0,7):\n",
    "        for i in range(1001):\n",
    "            vDraw=payoffs8[x+1][i]*(.001*i)+sum([(.001)*payoffs8[x+1][j] for j in range(i+1,1001)])\n",
    "            vNoDraw=payoffs8[x][i]\n",
    "            probDraw=np.exp(mu*(vDraw-max(vDraw,vNoDraw)))/(np.exp(mu*(vDraw-max(vDraw,vNoDraw)))+np.exp(mu*(vNoDraw-max(vDraw,vNoDraw))))\n",
    "            draws[x].append((epsilon)*probDraw+(1-epsilon)*(oldprobDraw[x][i]))\n",
    "            payoffs[x].append(vDraw*(probDraw)+vNoDraw*(1-probDraw))  \n",
    "\n",
    "    \n",
    "    return [draws,payoffs]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def period6(oldprobDraw,piold,payoffs7):\n",
    "    draws=[[],[],[],[],[],[]]\n",
    "    payoffs=[[],[],[],[],[],[]]\n",
    "\n",
    "    for x in range(0,6):\n",
    "        for i in range(1001):\n",
    "            vDraw=payoffs7[x+1][i]*(.001*i)+sum([(.001)*payoffs7[x+1][j] for j in range(i+1,1001)])\n",
    "            vNoDraw=payoffs7[x][i]\n",
    "            probDraw=np.exp(mu*(vDraw-max(vDraw,vNoDraw)))/(np.exp(mu*(vDraw-max(vDraw,vNoDraw)))+np.exp(mu*(vNoDraw-max(vDraw,vNoDraw))))\n",
    "            draws[x].append((epsilon)*probDraw+(1-epsilon)*(oldprobDraw[x][i]))\n",
    "            payoffs[x].append(vDraw*(probDraw)+vNoDraw*(1-probDraw))  \n",
    "\n",
    "    \n",
    "    return [draws,payoffs]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def period5(oldprobDraw,piold,payoffs6):\n",
    "    draws=[[],[],[],[],[]]\n",
    "    payoffs=[[],[],[],[],[]]\n",
    "\n",
    "    for x in range(0,5):\n",
    "        for i in range(1001):\n",
    "            vDraw=payoffs6[x+1][i]*(.001*i)+sum([(.001)*payoffs6[x+1][j] for j in range(i+1,1001)])\n",
    "            vNoDraw=payoffs6[x][i]\n",
    "            probDraw=np.exp(mu*(vDraw-max(vDraw,vNoDraw)))/(np.exp(mu*(vDraw-max(vDraw,vNoDraw)))+np.exp(mu*(vNoDraw-max(vDraw,vNoDraw))))\n",
    "            draws[x].append((epsilon)*probDraw+(1-epsilon)*(oldprobDraw[x][i]))\n",
    "            payoffs[x].append(vDraw*(probDraw)+vNoDraw*(1-probDraw))  \n",
    "\n",
    "    \n",
    "    return [draws,payoffs]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def period4(oldprobDraw,piold,payoffs5):\n",
    "    draws=[[],[],[],[]]\n",
    "    payoffs=[[],[],[],[]]\n",
    "\n",
    "    for x in range(0,4):\n",
    "        for i in range(1001):\n",
    "            vDraw=payoffs5[x+1][i]*(.001*i)+sum([(.001)*payoffs5[x+1][j] for j in range(i+1,1001)])\n",
    "            vNoDraw=payoffs5[x][i]\n",
    "            probDraw=np.exp(mu*(vDraw-max(vDraw,vNoDraw)))/(np.exp(mu*(vDraw-max(vDraw,vNoDraw)))+np.exp(mu*(vNoDraw-max(vDraw,vNoDraw))))\n",
    "            draws[x].append((epsilon)*probDraw+(1-epsilon)*(oldprobDraw[x][i]))\n",
    "            payoffs[x].append(vDraw*(probDraw)+vNoDraw*(1-probDraw))  \n",
    "\n",
    "    \n",
    "    return [draws,payoffs]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def period3(oldprobDraw,piold,payoffs4):\n",
    "    draws=[[],[],[]]\n",
    "    payoffs=[[],[],[]]\n",
    "\n",
    "    for x in range(0,3):\n",
    "        for i in range(1001):\n",
    "            vDraw=payoffs4[x+1][i]*(.001*i)+sum([(.001)*payoffs4[x+1][j] for j in range(i+1,1001)])\n",
    "            vNoDraw=payoffs4[x][i]\n",
    "            probDraw=np.exp(mu*(vDraw-max(vDraw,vNoDraw)))/(np.exp(mu*(vDraw-max(vDraw,vNoDraw)))+np.exp(mu*(vNoDraw-max(vDraw,vNoDraw))))\n",
    "            draws[x].append((epsilon)*probDraw+(1-epsilon)*(oldprobDraw[x][i]))\n",
    "            payoffs[x].append(vDraw*(probDraw)+vNoDraw*(1-probDraw))  \n",
    "\n",
    "    \n",
    "    return [draws,payoffs]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def period2(oldprobDraw,piold,payoffs3):\n",
    "    draws=[[],[]]\n",
    "    payoffs=[[],[]]\n",
    "\n",
    "    for x in range(0,2):\n",
    "        for i in range(1001):\n",
    "            vDraw=payoffs3[x+1][i]*(.001*i)+sum([(.001)*payoffs3[x+1][j] for j in range(i+1,1001)])\n",
    "            vNoDraw=payoffs3[x][i]\n",
    "            probDraw=np.exp(mu*(vDraw-max(vDraw,vNoDraw)))/(np.exp(mu*(vDraw-max(vDraw,vNoDraw)))+np.exp(mu*(vNoDraw-max(vDraw,vNoDraw))))\n",
    "            draws[x].append((epsilon)*probDraw+(1-epsilon)*(oldprobDraw[x][i]))\n",
    "            payoffs[x].append(vDraw*(probDraw)+vNoDraw*(1-probDraw))  \n",
    "\n",
    "    \n",
    "    return [draws,payoffs]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def period1(oldprobDraw,piold,payoffs2):\n",
    "    draws=[[]]\n",
    "    payoffs=[[]]\n",
    "\n",
    "    x=0\n",
    "    i=0\n",
    "    vDraw=payoffs2[x+1][i]*(.001*i)+sum([(.001)*payoffs2[x+1][j] for j in range(i+1,1001)])\n",
    "    vNoDraw=payoffs2[x][i]\n",
    "    probDraw=np.exp(mu*(vDraw-max(vDraw,vNoDraw)))/(np.exp(mu*(vDraw-max(vDraw,vNoDraw)))+np.exp(mu*(vNoDraw-max(vDraw,vNoDraw))))\n",
    "    draws[x].append((epsilon)*probDraw+(1-epsilon)*(oldprobDraw[x][i]))\n",
    "    payoffs[x].append(vDraw*(probDraw)+vNoDraw*(1-probDraw))  \n",
    "\n",
    "    \n",
    "    return [draws,payoffs]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Estimation**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gen_pi(draws1,draws2,draws3,draws4,draws5,draws6,draws7,draws8,draws9,draws10):\n",
    "    scores=[]\n",
    "    for i in range(10000):\n",
    "        score1=0        \n",
    "        draws=0\n",
    "\n",
    "        for j in range(10):\n",
    "\n",
    "            FS1=int(math.ceil((1-np.exp(-.125*score1))*1000))\n",
    "\n",
    "            if j==0:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws1[draws][FS1],draws1[draws][FS1]])\n",
    "            elif j==1:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws2[draws][FS1],draws2[draws][FS1]])\n",
    "            elif j==2:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws3[draws][FS1],draws3[draws][FS1]])\n",
    "            elif j==3:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws4[draws][FS1],draws4[draws][FS1]])\n",
    "            elif j==4:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws5[draws][FS1],draws5[draws][FS1]])\n",
    "            elif j==5:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws6[draws][FS1],draws6[draws][FS1]])\n",
    "            elif j==6:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws7[draws][FS1],draws7[draws][FS1]])\n",
    "            elif j==7:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws8[draws][FS1],draws8[draws][FS1]])\n",
    "            elif j==8:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws9[draws][FS1],draws9[draws][FS1]])\n",
    "            elif j==9:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws10[draws][FS1],draws10[draws][FS1]])\n",
    "\n",
    "            if drawP1==1:\n",
    "                score1=max(score1,np.random.exponential(8))\n",
    "                draws=draws+1\n",
    "\n",
    "        scores.append(score1)\n",
    "\n",
    "    scores=np.array(scores)\n",
    "\n",
    "    pinew=[]\n",
    "    # calculate the proportional values of samples\n",
    "    for i in range(1000):\n",
    "        pinew.append((len(scores[scores<(-np.log(1-.001*i)/.125)])/len(scores)))\n",
    "    pinew.append(1)\n",
    "    \n",
    "    return pinew"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "oldprobDraws10=[[(k<781)*1 for k in range(1001)] for j in range(10)]\n",
    "oldprobDraws9=[[(k<781)*1 for k in range(1001)] for j in range(9)]\n",
    "oldprobDraws8=[[(k<781)*1 for k in range(1001)] for j in range(8)]\n",
    "oldprobDraws7=[[(k<781)*1 for k in range(1001)] for j in range(7)]\n",
    "oldprobDraws6=[[(k<781)*1 for k in range(1001)] for j in range(6)]\n",
    "oldprobDraws5=[[(k<781)*1 for k in range(1001)] for j in range(5)]\n",
    "oldprobDraws4=[[(k<781)*1 for k in range(1001)] for j in range(4)]\n",
    "oldprobDraws3=[[(k<781)*1 for k in range(1001)] for j in range(3)]\n",
    "oldprobDraws2=[[(k<781)*1 for k in range(1001)] for j in range(2)]\n",
    "oldprobDraws1=[[(k<781)*1 for k in range(1001)] for j in range(1)]\n",
    "\n",
    "pinew=gen_pi(oldprobDraws1,oldprobDraws2,oldprobDraws3,oldprobDraws4,oldprobDraws5,oldprobDraws6,oldprobDraws7,oldprobDraws8,oldprobDraws9,oldprobDraws10)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def Estimation(IoldprobDraws1,IoldprobDraws2,IoldprobDraws3,IoldprobDraws4,IoldprobDraws5,IoldprobDraws6,IoldprobDraws7,IoldprobDraws8,IoldprobDraws9,IoldprobDraws10,Ipinew):\n",
    "    piold=[.001*i for i in range(1001)]\n",
    "\n",
    "    oldprobDraws10=IoldprobDraws10\n",
    "    oldprobDraws9=IoldprobDraws9\n",
    "    oldprobDraws8=IoldprobDraws8\n",
    "    oldprobDraws7=IoldprobDraws7\n",
    "    oldprobDraws6=IoldprobDraws6\n",
    "    oldprobDraws5=IoldprobDraws5\n",
    "    oldprobDraws4=IoldprobDraws4\n",
    "    oldprobDraws3=IoldprobDraws3\n",
    "    oldprobDraws2=IoldprobDraws2\n",
    "    oldprobDraws1=IoldprobDraws1\n",
    "    \n",
    "    pinew=Ipinew\n",
    "\n",
    "    while abs(sum(piold)-sum(pinew))>.25:\n",
    "\n",
    "        piold=pinew\n",
    "\n",
    "\n",
    "        [draws10,payoffs10]=period10(oldprobDraws10,piold)\n",
    "        [draws9,payoffs9]=period9(oldprobDraws9,piold,payoffs10)\n",
    "        [draws8,payoffs8]=period8(oldprobDraws8,piold,payoffs9)\n",
    "        [draws7,payoffs7]=period7(oldprobDraws7,piold,payoffs8)\n",
    "        [draws6,payoffs6]=period6(oldprobDraws6,piold,payoffs7)\n",
    "        [draws5,payoffs5]=period5(oldprobDraws5,piold,payoffs6)\n",
    "        [draws4,payoffs4]=period4(oldprobDraws4,piold,payoffs5)\n",
    "        [draws3,payoffs3]=period3(oldprobDraws3,piold,payoffs4)\n",
    "        [draws2,payoffs2]=period2(oldprobDraws2,piold,payoffs3)\n",
    "        [draws1,payoffs1]=period1(oldprobDraws1,piold,payoffs2)\n",
    "\n",
    "        pinew=gen_pi(draws1,draws2,draws3,draws4,draws5,draws6,draws7,draws8,draws9,draws10)\n",
    "\n",
    "        \n",
    "        print(abs(sum(piold)-sum(pinew)))\n",
    "        \n",
    "        oldprobDraws10=draws10\n",
    "        oldprobDraws9=draws9\n",
    "        oldprobDraws8=draws8\n",
    "        oldprobDraws7=draws7\n",
    "        oldprobDraws6=draws6\n",
    "        oldprobDraws5=draws5\n",
    "        oldprobDraws4=draws4\n",
    "        oldprobDraws3=draws3\n",
    "        oldprobDraws2=draws2\n",
    "        oldprobDraws1=draws1\n",
    "        \n",
    "        \n",
    "        \n",
    "    return [draws1,draws2,draws3,draws4,draws5,draws6,draws7,draws8,draws9,draws10,pinew]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Simulation**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def Simulation(draws1,draws2,draws3,draws4,draws5,draws6,draws7,draws8,draws9,draws10):\n",
    "    contest_draws=[]\n",
    "    for i in range(100000):\n",
    "        score1=0\n",
    "        score2=0\n",
    "        drawsFirst=0\n",
    "        drawsSecond=0\n",
    "\n",
    "        for j in range(10):\n",
    "\n",
    "            FS1=int(math.ceil((1-np.exp(-.125*score1))*1000))\n",
    "            FS2=int(math.ceil((1-np.exp(-.125*score2))*1000))\n",
    "\n",
    "\n",
    "            if j==0:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws1[drawsFirst][FS1],draws1[drawsFirst][FS1]])\n",
    "                drawP2=np.random.choice([0,1],size=1,p=[1-draws1[drawsSecond][FS2],draws1[drawsSecond][FS2]])\n",
    "\n",
    "            elif j==1:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws2[drawsFirst][FS1],draws2[drawsFirst][FS1]])\n",
    "                drawP2=np.random.choice([0,1],size=1,p=[1-draws2[drawsSecond][FS2],draws2[drawsSecond][FS2]])\n",
    "\n",
    "            elif j==2:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws3[drawsFirst][FS1],draws3[drawsFirst][FS1]])\n",
    "                drawP2=np.random.choice([0,1],size=1,p=[1-draws3[drawsSecond][FS2],draws3[drawsSecond][FS2]])\n",
    "\n",
    "            elif j==3:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws4[drawsFirst][FS1],draws4[drawsFirst][FS1]])\n",
    "                drawP2=np.random.choice([0,1],size=1,p=[1-draws4[drawsSecond][FS2],draws4[drawsSecond][FS2]])\n",
    "\n",
    "            elif j==4:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws5[drawsFirst][FS1],draws5[drawsFirst][FS1]])\n",
    "                drawP2=np.random.choice([0,1],size=1,p=[1-draws5[drawsSecond][FS2],draws5[drawsSecond][FS2]])\n",
    "\n",
    "            elif j==5:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws6[drawsFirst][FS1],draws6[drawsFirst][FS1]])\n",
    "                drawP2=np.random.choice([0,1],size=1,p=[1-draws6[drawsSecond][FS2],draws6[drawsSecond][FS2]])\n",
    "\n",
    "            elif j==6:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws7[drawsFirst][FS1],draws7[drawsFirst][FS1]])\n",
    "                drawP2=np.random.choice([0,1],size=1,p=[1-draws7[drawsSecond][FS2],draws7[drawsSecond][FS2]])\n",
    "\n",
    "            elif j==7:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws8[drawsFirst][FS1],draws8[drawsFirst][FS1]])\n",
    "                drawP2=np.random.choice([0,1],size=1,p=[1-draws8[drawsSecond][FS2],draws8[drawsSecond][FS2]])\n",
    "\n",
    "            elif j==8:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws9[drawsFirst][FS1],draws9[drawsFirst][FS1]])\n",
    "                drawP2=np.random.choice([0,1],size=1,p=[1-draws9[drawsSecond][FS2],draws9[drawsSecond][FS2]])\n",
    "\n",
    "            elif j==9:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws10[drawsFirst][FS1],draws10[drawsFirst][FS1]])\n",
    "                drawP2=np.random.choice([0,1],size=1,p=[1-draws10[drawsSecond][FS2],draws10[drawsSecond][FS2]])\n",
    "\n",
    "            if drawP1==1:\n",
    "                score1=max(score1,np.random.exponential(8))\n",
    "                drawsFirst=drawsFirst+1\n",
    "            if drawP2==1:\n",
    "                score2=max(score2,np.random.exponential(8))\n",
    "                drawsSecond=drawsSecond+1\n",
    "\n",
    "        contest_draws.append(drawsFirst+drawsSecond)\n",
    "\n",
    "    return np.array(contest_draws).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "draws_per_lamb=[]\n",
    "FinalProbsF=[]\n",
    "FinalProbsL=[]\n",
    "\n",
    "IoldprobDraws10=[[(k<781)*1 for k in range(1001)] for j in range(10)]\n",
    "IoldprobDraws9=[[(k<781)*1 for k in range(1001)] for j in range(9)]\n",
    "IoldprobDraws8=[[(k<781)*1 for k in range(1001)] for j in range(8)]\n",
    "IoldprobDraws7=[[(k<781)*1 for k in range(1001)] for j in range(7)]\n",
    "IoldprobDraws6=[[(k<781)*1 for k in range(1001)] for j in range(6)]\n",
    "IoldprobDraws5=[[(k<781)*1 for k in range(1001)] for j in range(5)]\n",
    "IoldprobDraws4=[[(k<781)*1 for k in range(1001)] for j in range(4)]\n",
    "IoldprobDraws3=[[(k<781)*1 for k in range(1001)] for j in range(3)]\n",
    "IoldprobDraws2=[[(k<781)*1 for k in range(1001)] for j in range(2)]\n",
    "IoldprobDraws1=[[(k<781)*1 for k in range(1001)] for j in range(1)]\n",
    "\n",
    "Ipinew=gen_pi(IoldprobDraws1,IoldprobDraws2,IoldprobDraws3,IoldprobDraws4,IoldprobDraws5,IoldprobDraws6,IoldprobDraws7,IoldprobDraws8,IoldprobDraws9,IoldprobDraws10)\n",
    "\n",
    "\n",
    "for lamb in [1+i*.05 for i in range(20)]:\n",
    "    [draws1,draws2,draws3,draws4,draws5,draws6,draws7,draws8,draws9,draws10,pinew]=Estimation(IoldprobDraws1,IoldprobDraws2,IoldprobDraws3,IoldprobDraws4,IoldprobDraws5,IoldprobDraws6,IoldprobDraws7,IoldprobDraws8,IoldprobDraws9,IoldprobDraws10,Ipinew)\n",
    "    average_draws=Simulation(draws1,draws2,draws3,draws4,draws5,draws6,draws7,draws8,draws9,draws10)\n",
    "    print(lamb,average_draws)\n",
    "    draws_per_lamb.append(average_draws)\n",
    "    FinalProbsF.append([draws1,draws2,draws3,draws4,draws5,draws6,draws7,draws8,draws9,draws10])\n",
    "    FinalProbsL.append([draws1,draws2,draws3,draws4,draws5,draws6,draws7,draws8,draws9,draws10])\n",
    "    \n",
    "    IoldprobDraws10=draws10\n",
    "    IoldprobDraws9=draws9\n",
    "    IoldprobDraws8=draws8\n",
    "    IoldprobDraws7=draws7\n",
    "    IoldprobDraws6=draws6\n",
    "    IoldprobDraws5=draws5\n",
    "    IoldprobDraws4=draws4\n",
    "    IoldprobDraws3=draws3\n",
    "    IoldprobDraws2=draws2\n",
    "    IoldprobDraws1=draws1\n",
    "\n",
    "    Ipinew=gen_pi(draws1,draws2,draws3,draws4,draws5,draws6,draws7,draws8,draws9,draws10)\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('Private_Loss.pkl', 'wb') as f:\n",
    "    pickle.dump(draws_per_lamb, f)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Simulation For Each Factor**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Private_Loss_Data=pd.DataFrame(columns=[\"Simulation Number\",\"Factor\",\"Coefficient Value\",\"Number of Draws\",\"Treatment\"])\n",
    "\n",
    "N=2\n",
    "T=10\n",
    "\n",
    "location=0\n",
    "for loss in range(19):\n",
    "    \n",
    "    [draws1,draws2,draws3,draws4,draws5,draws6,draws7,draws8,draws9,draws10]=FinalProbsF[loss]\n",
    "            \n",
    "    for i in range(10000):\n",
    "        score1=0\n",
    "        score2=0\n",
    "        drawsFirst=0\n",
    "        drawsSecond=0\n",
    "\n",
    "        for j in range(10):\n",
    "\n",
    "            FS1=int(math.ceil((1-np.exp(-.125*score1))*1000))\n",
    "            FS2=int(math.ceil((1-np.exp(-.125*score2))*1000))\n",
    "\n",
    "\n",
    "            if j==0:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws1[drawsFirst][FS1],draws1[drawsFirst][FS1]])\n",
    "                drawP2=np.random.choice([0,1],size=1,p=[1-draws1[drawsSecond][FS2],draws1[drawsSecond][FS2]])\n",
    "\n",
    "            elif j==1:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws2[drawsFirst][FS1],draws2[drawsFirst][FS1]])\n",
    "                drawP2=np.random.choice([0,1],size=1,p=[1-draws2[drawsSecond][FS2],draws2[drawsSecond][FS2]])\n",
    "\n",
    "            elif j==2:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws3[drawsFirst][FS1],draws3[drawsFirst][FS1]])\n",
    "                drawP2=np.random.choice([0,1],size=1,p=[1-draws3[drawsSecond][FS2],draws3[drawsSecond][FS2]])\n",
    "\n",
    "            elif j==3:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws4[drawsFirst][FS1],draws4[drawsFirst][FS1]])\n",
    "                drawP2=np.random.choice([0,1],size=1,p=[1-draws4[drawsSecond][FS2],draws4[drawsSecond][FS2]])\n",
    "\n",
    "            elif j==4:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws5[drawsFirst][FS1],draws5[drawsFirst][FS1]])\n",
    "                drawP2=np.random.choice([0,1],size=1,p=[1-draws5[drawsSecond][FS2],draws5[drawsSecond][FS2]])\n",
    "\n",
    "            elif j==5:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws6[drawsFirst][FS1],draws6[drawsFirst][FS1]])\n",
    "                drawP2=np.random.choice([0,1],size=1,p=[1-draws6[drawsSecond][FS2],draws6[drawsSecond][FS2]])\n",
    "\n",
    "            elif j==6:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws7[drawsFirst][FS1],draws7[drawsFirst][FS1]])\n",
    "                drawP2=np.random.choice([0,1],size=1,p=[1-draws7[drawsSecond][FS2],draws7[drawsSecond][FS2]])\n",
    "\n",
    "            elif j==7:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws8[drawsFirst][FS1],draws8[drawsFirst][FS1]])\n",
    "                drawP2=np.random.choice([0,1],size=1,p=[1-draws8[drawsSecond][FS2],draws8[drawsSecond][FS2]])\n",
    "\n",
    "            elif j==8:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws9[drawsFirst][FS1],draws9[drawsFirst][FS1]])\n",
    "                drawP2=np.random.choice([0,1],size=1,p=[1-draws9[drawsSecond][FS2],draws9[drawsSecond][FS2]])\n",
    "\n",
    "            elif j==9:\n",
    "                drawP1=np.random.choice([0,1],size=1,p=[1-draws10[drawsFirst][FS1],draws10[drawsFirst][FS1]])\n",
    "                drawP2=np.random.choice([0,1],size=1,p=[1-draws10[drawsSecond][FS2],draws10[drawsSecond][FS2]])\n",
    "\n",
    "            if drawP1==1:\n",
    "                score1=max(score1,np.random.exponential(8))\n",
    "                drawsFirst=drawsFirst+1\n",
    "            if drawP2==1:\n",
    "                score2=max(score2,np.random.exponential(8))\n",
    "                drawsSecond=drawsSecond+1\n",
    "\n",
    "\n",
    "        Private_Loss_Data.loc[location]=[iter,\"Loss Aversion Coefficient\",.05*loss,drawsFirst+drawsSecond,\"Private\"]\n",
    "        location=location+1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Private_Loss_Data.groupby(\"Coefficient Value\")[\"Number of Draws\"].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Private_Loss_Data.to_csv(\"Private_Loss_Data.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
